package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelCollect;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.TravelCollectMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ITravelCollectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelCollectServiceimpl implements ITravelCollectService {
    @Autowired
    private TravelCollectMapper travelCollectMapper;

    @Override
    public TravelCollect getCollectByUserId(Long userId, Long travelId,Long state) {
        TravelCollect travelCollect = travelCollectMapper.getCollectByUserId(userId,travelId,state);
        return travelCollect;
    }

    @Override
    public boolean selectCollect(Long userId, Long travelId) {
        TravelCollect travelCollect = travelCollectMapper.selectExist(userId,travelId);
        boolean flag = true;
        Date time = new Date();
        if(travelCollect != null){
            boolean state = travelCollect.isState();
            state = !state;
            travelCollectMapper.changeState(userId,travelId,time,state);
            flag = state;
        }else{
            TravelCollect entity = new TravelCollect();
            entity.setTime(time);
            User user = new User();
            user.setId(userId);
            entity.setUser(user);
            Travel travel = new Travel();
            travel.setId(travelId);
            entity.setUser(user);
            entity.setTravel(travel);
            entity.setState(true);
            travelCollectMapper.insertCollect(entity);
            flag = true;
        }
        return flag;
    }

    @Override
    public PageInfo<TravelCollect> query(QueryObject qo,Long userId) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<TravelCollect> list = travelCollectMapper.query(qo,userId);
        return new PageInfo<>(list);
    }
}
